package com.modular.iot.tool;

import cn.hutool.core.lang.Opt;
import cn.hutool.core.util.ObjectUtil;
import com.c.rabbit_mq.impl.IotCmdExe;
import com.c.opt.ComOpt;
import com.ToolOpt;
import com.modular.iot.opt.OptIotDevice;
import com.modular.iot.bean.ThingsDeviceCmdDo;
import ntd.util.config.common.exception.ComException;
import ntd.util.modular.data.util.handle.tool.Tool212;
import ntd.util.modular.things.entity.Device;

import java.util.List;

/**
 * 设备指令工具
 */
public class ToolIotCmd {

    public static final String TYPE_212 = "212";

    private static ComOpt<ThingsDeviceCmdDo> thingsDeviceCmdDoComOpt = ToolOpt.OPT_T_D_CMD;
    private static OptIotDevice OPT_T_DEVICE = ToolOpt.OPT_T_DEVICE;

    /**
     * 推送指令
     * @param deviceCode
     * @param cmd
     */
    
    public static void pushCmd(String deviceCode, String cmd, String type, String opt) {
        Device device = OPT_T_DEVICE.query().eq("code", deviceCode).eq("status", "1").getBean();
        if (ObjectUtil.isNull(device)) {
            throw new ComException("当前设备不在线，请等待设备在线后进行操作...");
        }
        // 验证设备指令状态
//        List<ThingsDeviceCmdDo> thingsDeviceCmdDos = thingsDeviceCmdDoComOpt.query().eq("device_code", deviceCode)
//                .eq("status", "0")
//                .ge("created_date", DateUtil.offsetSecond(DateUtil.date(), -30).toString())
//                .listBean();
//        if (CollUtil.isNotEmpty(thingsDeviceCmdDos)) {
//            throw new ComException("存在执行中指令，请等待执行完成进行操作...");
//        }
        ThingsDeviceCmdDo thingsDeviceCmdDo = new ThingsDeviceCmdDo();
        thingsDeviceCmdDo.setDeviceCode(Opt.ofBlankAble(deviceCode).orElseThrow(() -> new ComException("推送指令异常，设备编码不能为空...")));
        thingsDeviceCmdDo.setCmd(Opt.ofBlankAble(cmd).orElseThrow(() -> new ComException("推送指令异常，设备指令不能为空...")));
        thingsDeviceCmdDo.setStatus("0");
        thingsDeviceCmdDo.setType(Opt.ofBlankAble(type).orElseThrow(() -> new ComException("推送指令异常，指令类型不能为空...")));
        thingsDeviceCmdDo.setOpt(Opt.ofBlankAble(opt).orElseThrow(() -> new ComException("推送指令异常，指令操作不能为空...")));
        thingsDeviceCmdDo.setId(thingsDeviceCmdDoComOpt.syncBean(thingsDeviceCmdDo));

        // 推送 mqtt 消息
        IotCmdExe.push(thingsDeviceCmdDo);
//        ComMqttManager.pushMsg(IotCmdExe.TOPIC + "/" + thingsDeviceCmdDo.getDeviceCode(), JSONUtil.toJsonStr(thingsDeviceCmdDo));
    }

    
    public static List<ThingsDeviceCmdDo> listCmd() {
        return thingsDeviceCmdDoComOpt.listBean(thingsDeviceCmdDoComOpt.query().eq("status", "0"));
    }

    public static ThingsDeviceCmdDo getCmdByCode(String code) {
        return thingsDeviceCmdDoComOpt.query()
                .eq("status", "0")
                .eq("device_code", Opt.ofBlankAble(code).orElseThrow(() -> new ComException("code 不能为空")))
                .getBean();
    }
    
    public static void exeSuccess(ThingsDeviceCmdDo thingsDeviceCmdDo) {
        thingsDeviceCmdDo.setStatus("1");
        thingsDeviceCmdDo.setNum(thingsDeviceCmdDo.getNum() + 1);
        thingsDeviceCmdDoComOpt.syncBean(thingsDeviceCmdDo);
    }

    public static void exeError(ThingsDeviceCmdDo thingsDeviceCmdDo) {
        thingsDeviceCmdDo.setNum(thingsDeviceCmdDo.getNum() + 1);
        thingsDeviceCmdDo.setStatus(null);
        if (thingsDeviceCmdDo.getNum() > 5) {
            thingsDeviceCmdDo.setStatus("3");
        }
        thingsDeviceCmdDoComOpt.syncBean(thingsDeviceCmdDo);
    }

    /**
     * 执行指令
     * @param thingsDeviceCmdDo
     */
    public static void exeCmd(ThingsDeviceCmdDo thingsDeviceCmdDo) {
        if (thingsDeviceCmdDo.getNum() > 10) {
            return;
        }
        if (ToolIotCmd.TYPE_212.equals(thingsDeviceCmdDo.getType())) {
            if (Tool212.isOnLine(thingsDeviceCmdDo.getDeviceCode())) {
                if (Tool212.sendMsg(thingsDeviceCmdDo.getDeviceCode(), thingsDeviceCmdDo.getCmd())) {
                    ToolIotCmd.exeSuccess(thingsDeviceCmdDo);
                } else {
                    ToolIotCmd.exeError(thingsDeviceCmdDo);
                }
            }
        }
    }
}
